package com.cg.controller.business;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.cg.entity.AppVersion;
import com.cg.entity.BusinessFile;
import com.cg.service.BusinessFileService;
import com.cg.utils.DateUtils;
import com.cg.utils.Query;
import com.cg.utils.R;
import com.cg.utils.UploadUtil;
import com.cg.utils.annotation.SysLog;


@RestController
@RequestMapping("/business/file/")
public class BusinessFileController {
	/**访问地址*/
	@Value("${global.upload.path}")
	private String path;
	/**存放地址*/
	@Value("${global.upload.docBase}")
	private String docBase;
	@Autowired
	BusinessFileService businessFileService;
	@Autowired
	UploadUtil uploadUtil;
	private String mypath="basefile/";
	
	/**
	 * 所有文件列表
	 */
	@RequestMapping("list")
	@RequiresPermissions("business:file:list")
	public R list(@RequestParam Map<String, Object> params){
		//查询列表数据
		Query query = new Query(params);
		List<BusinessFile> fileList = businessFileService.queryList(query);
		int total = businessFileService.queryTotal(query);
		
		return R.success(total, "", fileList);
	}
	/**
	 * 信息
	 */
	@RequestMapping("/info/{id}")
	@RequiresPermissions("business:file:info")
	public R info(@PathVariable("id") Integer id){
		BusinessFile businessFile = businessFileService.queryObjectById(id);
		
		
		return R.ok().put("businessfile", businessFile);
	}
	
	/**
	 * 保存带
	 */
	@SysLog("保存文件")
	@RequestMapping("save")
	@RequiresPermissions("business:file:save")
	public R save(HttpServletRequest request,@RequestBody BusinessFile businessFile){
//		List<String> filePath = uploadUtil.upload(request,docBase, path,"businessFile/");
//		BusinessFile file = new BusinessFile();
		/*String name = request.getParameter("name");
		Integer parentId ;
		if(!StringUtils.isBlank(req.getParameter("parentId"))){
			parentId = Integer.parseInt(req.getParameter("parentId"));
			menu.setType(1);
		}else{
			parentId = 0;
			menu.setType(0);
		}
		Integer enable = Integer.parseInt(req.getParameter("enable"));
		menu.setName(name);
		menu.setParentId(parentId);
		menu.setCreateTime(new Date());
		menu.setUpdateTime(new Date());
		menu.setEnable(enable);
		menu.setImgUrl(filePath.get(0));
		businessFileService.save(menu);*/
		businessFile.setCreatetime(DateUtils.dateToDateString(new Date()));
		businessFileService.save(businessFile);
		return R.ok();
	}
	
	/**
	 * 保存带图片
	 */
	@SysLog("保存")
	@RequestMapping("/save2")
	@RequiresPermissions("business:file:save")
	public R save2(HttpServletRequest request, BusinessFile businessFile){
		//数据校验
		String filePath = uploadUtil.uploadImg(request,mypath);
		businessFile.setUrl(filePath);
		businessFile.setCreatetime(DateUtils.dateToDateString(new Date()));
		businessFile.setUpdatetime(DateUtils.dateToDateString(new Date()));
		businessFileService.save(businessFile);
		
		return R.ok();
	}
	
	/**
	 * 修改带图片
	 */
	@SysLog("修改")
	@RequestMapping("/update2")
	@RequiresPermissions("business:file:update")
	public R update2(HttpServletRequest request, BusinessFile businessFile){
		//数据校验
//		BusinessFile businessFile = new BusinessFile ();
//		businessFile.setCreatetime(DateUtils.dateToDateString(new Date()));
		String filePath = uploadUtil.uploadImg(request,mypath);
//		businessFile.setUrl(filePath);
//		businessFileService.update(businessFile);
		
		return R.ok(filePath);
	}
	
	/**
	 * 修改带图片
	 */
	@SysLog("修改")
	@RequestMapping("/update")
	@RequiresPermissions("business:file:update")
	public R update(HttpServletRequest request,@RequestBody BusinessFile businessFile){
//		HttpServletRequest req = XssHttpServletRequestWrapper.getOrgRequest(request);
//		List<String> filePath = uploadUtil.upload(req,docBase, path,"appmenu/");
//		AppMenuEntity menu = new AppMenuEntity();
//		Integer id = Integer.parseInt(req.getParameter("id"));
//		String name = req.getParameter("name");
//		Integer parentId ;
//		if(!StringUtils.isBlank(req.getParameter("parentId"))){
//			parentId = Integer.parseInt(req.getParameter("parentId"));
//			menu.setType(1);
//		}else{
//			parentId = 0;
//			menu.setType(0);
//		}
//		Integer enable = Integer.parseInt(req.getParameter("enable"));
//		menu.setId(id);
//		menu.setName(name);
//		menu.setParentId(parentId);
//		menu.setUpdateTime(new Date());
//		menu.setEnable(enable);
//		menu.setImgUrl(filePath.get(0));
		businessFile.setCreatetime(DateUtils.dateToDateString(new Date()));
		businessFileService.update(businessFile);
		return R.ok();
	}
	
	/**
	 * 删除
	 */
	@SysLog("删除文件")
	@RequestMapping("delete")
	@RequiresPermissions("business:file:delete")
	public R delete(@RequestBody Integer[] menuIds){
		businessFileService.deleteBatch(menuIds);
		return R.ok();
	}

}
